//
//  AppDelegate.m
//  NeteaseNewsLY
//
//  Created by dllo on 16/1/16.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "AppDelegate.h"

#import "ReadViewController.h"
#import "TopicViewController.h"
#import "MyViewController.h"
#import "VideoViewController.h"
#import "UMSocial.h"
#import "HomeViewController.h"

#import "UMSocialControllerService.h"

#import "LeadPageVC.h"
@interface AppDelegate ()
@property (nonatomic, assign)BOOL isOut;
@end

@implementation AppDelegate

- (void)dealloc
{
    [_window release];
    [super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen]bounds]];
    
    [UMSocialData setAppKey:@"56a87642e0f55ae402002a71"];
    
    // 启动页停留一段时间
    [NSThread sleepForTimeInterval:2.0];
     
    BOOL isFirst = [[NSUserDefaults standardUserDefaults]boolForKey:@"firstLaunch"];
    // 判断是否是第一次打开
    if (!isFirst) {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];
        LeadPageVC *lead = [[LeadPageVC alloc] init];
        // 如果是第一次打开将引导页VC设置为window的根视图
        self.window.rootViewController = lead;
        
        // 创建VC
        HomeViewController *rootVC = [[HomeViewController alloc] init];
        // 导航
        UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:rootVC];
        // tabbar
        self.tabBar = [[UITabBarController alloc] init];
        // 设置tabBar
        rootVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"新闻" image:[UIImage imageNamed:@"xinwen@2x"] tag:1000];
        //read
        ReadViewController *readVC = [[ReadViewController alloc] init];
        UINavigationController *navi2 = [[UINavigationController alloc] initWithRootViewController:readVC];
        readVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"阅读" image:[UIImage imageNamed:@"read@2x"] tag:2000];
        // topic
        TopicViewController *topicVC = [[TopicViewController alloc] init];
        UINavigationController *navi3 = [[UINavigationController alloc] initWithRootViewController:topicVC];
        topicVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"话题" image:[UIImage imageNamed:@"topic@2x"] tag:3000];
        // my
        MyViewController *myVC = [[MyViewController alloc] init];
        UINavigationController *navi4 = [[UINavigationController alloc] initWithRootViewController:myVC];
        myVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我" image:[UIImage imageNamed:@"my@2x"] tag:4000];
        // 视频
        VideoViewController *videoVC = [[VideoViewController alloc] init];
        UINavigationController *navi5 = [[UINavigationController alloc] initWithRootViewController:videoVC];
        videoVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"视听" image:[UIImage imageNamed:@"shipin@2x"] tag:5000];
        
        
        _tabBar.tabBar.tintColor = [UIColor redColor];
        
        _tabBar.viewControllers = @[navi, navi2, navi5, navi3, navi4];
//        self.window.rootViewController = _tabBar;
        
        [rootVC release];
        [readVC release];
        [topicVC release];
        [myVC release];
        [videoVC release];
        [navi release];
        [navi2 release];
        [navi3 release];
        [navi4 release];
        [navi5 release];
        [_tabBar release];
        [_window release];
        [rootVC.tabBarItem release];
        [readVC.tabBarItem release];
        [topicVC.tabBarItem release];
        [myVC.tabBarItem release];
        [videoVC.tabBarItem release];
        
    }else{
        // 创建VC
        HomeViewController *rootVC = [[HomeViewController alloc] init];
        // 导航
        UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:rootVC];
        // tabbar
        self.tabBar = [[UITabBarController alloc] init];
        // 设置tabBar
        rootVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"新闻" image:[UIImage imageNamed:@"xinwen@2x"] tag:1000];
        //read
        ReadViewController *readVC = [[ReadViewController alloc] init];
        UINavigationController *navi2 = [[UINavigationController alloc] initWithRootViewController:readVC];
        readVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"阅读" image:[UIImage imageNamed:@"read@2x"] tag:2000];
        // topic
        TopicViewController *topicVC = [[TopicViewController alloc] init];
        UINavigationController *navi3 = [[UINavigationController alloc] initWithRootViewController:topicVC];
        topicVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"话题" image:[UIImage imageNamed:@"topic@2x"] tag:3000];
        // my
        MyViewController *myVC = [[MyViewController alloc] init];
        UINavigationController *navi4 = [[UINavigationController alloc] initWithRootViewController:myVC];
        myVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我" image:[UIImage imageNamed:@"my@2x"] tag:4000];
        // 视频
        VideoViewController *videoVC = [[VideoViewController alloc] init];
        UINavigationController *navi5 = [[UINavigationController alloc] initWithRootViewController:videoVC];
        videoVC.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"视听" image:[UIImage imageNamed:@"shipin@2x"] tag:5000];
        
        
        _tabBar.tabBar.tintColor = [UIColor redColor];
        
        _tabBar.viewControllers = @[navi, navi2, navi5, navi3, navi4];
        self.window.rootViewController = _tabBar;
        
        [rootVC release];
        [readVC release];
        [topicVC release];
        [myVC release];
        [videoVC release];
        [navi release];
        [navi2 release];
        [navi3 release];
        [navi4 release];
        [navi5 release];
        [_tabBar release];
        [_window release];
        [rootVC.tabBarItem release];
        [readVC.tabBarItem release];
        [topicVC.tabBarItem release];
        [myVC.tabBarItem release];
        [videoVC.tabBarItem release];


    }
    
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}
#pragma mark - 接收本地推送
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    NSLog(@"推送");
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"News" message:notification.alertBody delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
    alert.delegate = self;
    [alert show];
    application.applicationIconBadgeNumber -= 1;
}
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
